package com.hejia.bulbul.scheduler;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import com.hejia.alauda.entity.PushMessage;
import com.hejia.alauda.service.PushMessageService;
import com.umeng.config.UmengConfig;

/**
 * 推送消息作业，推送指定时间推送的消息。
 * 
 * @author hyr
 *
 */
public class PushMessageScheduler {

	private static final Log logger = LogFactory.getLog(PushMessageScheduler.class);
	
	@Resource
	private UmengConfig umengConfig;
	
	@Resource
	private PushMessageService pushMessageService;
	
	/**
	 * 执行推送
	 */
	// [秒] [分] [小时] [日] [月] [周] [年]
	@Scheduled(fixedDelay = 1000 * 60 * 10)
	@Async("executor")
	public void push() {
		try {
			logger.info("开始推送定时消息作业");
			List<PushMessage> pushMessages = this.pushMessageService.findTimingPushMessages();
			for (PushMessage pushMessage : pushMessages) {
				this.pushMessageService.pushTimingMessage(umengConfig, pushMessage.getId());
			}
			logger.info("结束推送定时消息作业");
		} catch (Exception e) {
			logger.error("发送定时推送消息错误", e);
		}
	}
}
